package database;

import java.sql.Connection;
import java.sql.*;
import java.util.HashSet;
import plogg.*;
// These will be used when the database is running

/*import java.sql.SQLException;		Not in use yet
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;*/

import plogg.Plogg;

public class Database {
	// Constants *************************************************************
	private final String DBMS = "jdbc:sqlite:";
	
	// Instance Variables **************************************************** 
	private String mPath;  // This should be the path to the database
	private Connection mConnect;
	
	// Constructors **********************************************************
	
	/**
	 * This constructor could be given a path to a database
	 */
	public Database(String pDBName) {
		mPath = DBMS + pDBName;  // We can feed this a test string
	}
	
	// Methods ***************************************************************
	
	/**
     * Attempt to open the database
     * @return true if the connection is successful,
     * false if not
     */
	public boolean openDatabase()
	{
		try
		{
             Class.forName("org.sqlite.JDBC"); // Loads the JBDC driver
             mConnect = DriverManager.getConnection(mPath);
             return true; //if it reaches this point, it's connected
		}
		catch (Exception e)
		{
			return false;
		}
	}

	/**
     * Closes the database
     * @return true if the database was closed
     * successfully, false if not
     */
	public boolean closeDatabase()
	{
		try {
			if (mConnect != null)
			{
	             mConnect.close();
	             return true;
			}
		} catch(SQLException e) {
			System.out.println(e + "DB not found");
			return false;
		}	
		return false;
	}
	
	
	// Stub function should return all appropriate Plogg IDs
	public static HashSet<String> getAllDevices()	{
		HashSet<String> fakelist = new HashSet<String>();
		fakelist.add("b1:00:12:a5:5b");
		fakelist.add("e1:00:00:a5:5b");
		fakelist.add("e1:00:22:a6:5b");
		return fakelist;
	}
	
	/**
	 * Stub function to connect to the Database
	 */
	public static void connectToDatabase() {
		/* Dummy call
		 * Will be modified when MySQL database is up and running
		 * on a server
		 */
		
		System.out.println("Database connection successful");
	}
	
	/**
	 * Stub function to register a new plogg in the database
	 * @param p Plogg device
	 */
	public static void registerNewPlogg(Plogg p) {
		System.out.println(p);
	}
	
	/**
	 * Stub method that will call on the database to find the
	 * appropriate Plogg, and return that instance
	 * @param ploggID ID of the Plogg
	 * @return Plogg with which that ID is registered
	 */
	public static Plogg findPloggDevice(String ploggID) {
		/*
		 * Will call on the database to retrieve the plogg.
		 * For now, will return a new Plogg instance with 
		 * dummy data
		 */
		return new Plogg(ploggID);
	}
	
	/**
	 * Returns a reading for a particular Plogg??
	 * This will work for date ranges - possibly overload
	 * this method?
	 */
	public static HashSet<String> getPloggReading() {
		
		HashSet<String> readings = new HashSet<String>();
		readings.add("Mon 12 Jan 2008, kWh: 120, Total: 560");
		readings.add("Tue 13 Jan 2008, kWh: 148, Total: 642");
		readings.add("Wed 14 Jan 2008, kWh: 152, Total: 741");
		return readings;
	}
	
	/**
	 * Adds a new reading into the database. Would we
	 * need a readings object?
	 * public static void addNewReading(Reading reading) ??
	 */
	public static void addNewReading() {
	
		System.out.println("Reading added");
		
	}
	
	/**
	 * Sets the plogg's Name in the database
	 * @param name New name for the plogg
	 * @param ploggID ID of the plogg
	 * @return true if successful, false if not
	 */
	public static boolean setPloggName(String name, String ploggID) {
		
		/*
		 * Will retrieve the relevant plogg and run an update
		 * command on it to change its name
		 */
		System.out.println("Plogg ID: " + ploggID + " now called " + name);
		return true;
	}
}
